package com.cta.view.model.discount;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;

import com.cta.entity.DiscountRate;
import com.cta.view.model.CallData;

public class HourDiscountModel extends DiscountModel {

	private LocalTime from;
	private LocalTime to;
	
	private DateTime dateTo;
	private DateTime dateFrom;

	public HourDiscountModel(DiscountRate orgDiscount) {
		super(orgDiscount);
		if (StringUtils.isNotEmpty(orgDiscount.getCondition())) {
			String[] time = orgDiscount.getCondition().split("-");
			from = LocalTime.parse(time[0]);
			to = LocalTime.parse(time[1]);
		}
	}

	@Override
	public void hasDiscount(CallData data) throws Exception {
		DateTime startDate = data.getStartDate();
		DateTime endDate = data.getEndDate();
		// using start date as standard day
		DateTime date = data.getDate();
		dateFrom = date.plusHours(from.getHourOfDay()).plusMinutes(from.getMinuteOfHour())
				.plusSeconds(from.getSecondOfMinute());
		if (to.compareTo(from) <= 0) {
			// split to 2 day
			dateTo = date.plusDays(1).plusHours(to.getHourOfDay()).plusMinutes(to.getMinuteOfHour())
					.plusSeconds(to.getSecondOfMinute());
		} else {
			dateTo = date.plusHours(to.getHourOfDay()).plusMinutes(to.getMinuteOfHour())
					.plusSeconds(to.getSecondOfMinute());
		}

		if(!(endDate.compareTo(dateFrom) <= 0 || startDate.compareTo(dateTo) >= 0)){
			log.info(String.format("Has hours discount-> dateFrom: %s, dateTo: %s", dateFrom, dateTo));
			data.getHourDiscounts().put(orgDiscount.getCondition(), this);
		}
	}

	public LocalTime getFrom() {
		return from;
	}

	public void setFrom(LocalTime from) {
		this.from = from;
	}

	public LocalTime getTo() {
		return to;
	}

	public void setTo(LocalTime to) {
		this.to = to;
	}

	public DateTime getDateTo() {
		return dateTo;
	}

	public void setDateTo(DateTime dateTo) {
		this.dateTo = dateTo;
	}

	public DateTime getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(DateTime dateFrom) {
		this.dateFrom = dateFrom;
	}

}
